﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Logistics_Management_System.Customer_Employee_Permission
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Employee_Service" in code, svc and config file together.
    public class Employee_Service : ServiceBase, IEmployee_Service
    {
        private Employee_Repository emp_repo = new Employee_Repository();
        private Permission_Repository per_repo = new Permission_Repository();

        public Objects.LoginResult EmployeeLogin(string username, string password)
        {
            var existing = emp_repo.GetEmployeeByUsername(username);
            if (existing != null)
            {
                if (existing.Password.Equals(emp_repo.encodeMd5(password)))
                {
                    //Get roles id by employee
                    var emp_role = (from e_r in this.DB.LG_Employee_Roles where e_r.Emp_Id == existing.Id select e_r).ToList<LG_Employee_Role>();
                    List<LG_Permission> list_per = new List<LG_Permission>();
                    foreach (var role in emp_role)
                    {
                        var role_per = (from r_p in this.DB.LG_Role_Permissions where r_p.Role_Id == role.Role_Id select r_p).ToList<LG_Role_Permission>();
                        foreach (var per in role_per)
                        {
                            var permission = (from p in this.DB.LG_Permissions where p.Id == per.Per_Id select p).FirstOrDefault<LG_Permission>();
                            var existing_per = (from ep in list_per where ep.Id == permission.Id select ep).FirstOrDefault<LG_Permission>();
                            if (existing_per == null) list_per.Add(permission);
                        }
                    }
                    return new Objects.LoginResult() { Value = true, Message = "Success Login", Permissions = list_per };
                }
                else { return new Objects.LoginResult() { Value = false, Message = "Inconrrect password!!" }; }
            }
            else
            { return new Objects.LoginResult() { Value = false, Message = "Username is not exist.." }; }
        }
        public List<LG_Employee> GetAllEmployees()
        {
            return (from emp in this.DB.LG_Employees select emp).ToList<LG_Employee>();
        }
        public Objects.ReturnBool AddNewEmployee(LG_Employee new_emp)
        {
            if (emp_repo.GetEmployeeByUsername(new_emp.Username) == null)
            {
                this.DB.LG_Employees.InsertOnSubmit(new_emp);
                this.DB.SubmitChanges();
                return new Objects.ReturnBool() { Value = true };
            }
            return new Objects.ReturnBool() { Value = false, Reason = "Username is exist..." };
        }
        public Objects.ReturnBool EditInfoEmployee(int emp_id, LG_Employee updated)
        {
            var existing = emp_repo.GetEmployeeById(emp_id);
            if (existing != null)
            {
                if (emp_repo.GetEmployeeByUsername(updated.Username) == null)
                {
                    this.DB.LG_Employees.Attach(existing);
                    if (updated.Branch_Id != 0)
                        existing.Branch_Id = updated.Branch_Id;
                    if (!String.IsNullOrEmpty(updated.Username))
                        existing.Username = updated.Username;
                    if (!String.IsNullOrEmpty(updated.Password))
                        existing.Password = updated.Password;
                    if (!String.IsNullOrEmpty(updated.First_Name))
                        existing.First_Name = updated.First_Name;
                    if (!String.IsNullOrEmpty(updated.Last_Name))
                        existing.Last_Name = updated.Last_Name;
                    if (updated.Sex != null)
                        existing.Sex = updated.Sex;

                    if (updated.Birth_Date != null)
                        existing.Birth_Date = updated.Birth_Date;
                    if (updated.Hire_Date != null)
                        existing.Hire_Date = updated.Hire_Date;
                    if (!String.IsNullOrEmpty(updated.Address))
                        existing.Address = updated.Address;
                    if (!String.IsNullOrEmpty(updated.City))
                        existing.City = updated.City;
                    if (!String.IsNullOrEmpty(updated.Post_Code))
                        existing.Post_Code = updated.Post_Code;
                    if (!String.IsNullOrEmpty(updated.Country))
                        existing.Country = updated.Country;
                    if (!String.IsNullOrEmpty(updated.Phone))
                        existing.Phone = updated.Phone;
                    if (!String.IsNullOrEmpty(updated.Email))
                        existing.Email = updated.Email;
                    if (!String.IsNullOrEmpty(updated.Photo))
                        existing.Photo = updated.Photo;
                    if (!String.IsNullOrEmpty(updated.Notes))
                        existing.Notes = updated.Notes;
                    this.DB.SubmitChanges();
                    return new Objects.ReturnBool() { Value = true };
                }
                else
                {
                    return new Objects.ReturnBool() { Value = false, Reason = "Username is exist.." };
                }
            }
            else
            {
                return new Objects.ReturnBool() { Value = false, Reason = "Can't found the employee by this id" };
            }
        }
        public Objects.ReturnBool RemoveEmployee(int emp_id)
        {
            var existing = emp_repo.GetEmployeeById(emp_id);
            if (existing != null)
            {
                this.DB.LG_Employees.Attach(existing);
                this.DB.LG_Employees.DeleteOnSubmit(existing);
                this.DB.SubmitChanges();
                return new Objects.ReturnBool() { Value = true };
            }
            return new Objects.ReturnBool() { Value = false, Reason = "Can't found employee by this id" };
        }
        public Objects.ReturnBool ChangePassword(int emp_id, string old_password, string new_password)
        {
            var existing = (from emp in this.DB.LG_Employees where emp.Id == emp_id select emp).FirstOrDefault<LG_Employee>();
            if (existing != null)
            {
                if (existing.Password == emp_repo.encodeMd5(old_password))
                {
                    existing.Password = emp_repo.encodeMd5(new_password);
                    this.DB.SubmitChanges();
                    return new Objects.ReturnBool() { Value = true };
                }
                else { return new Objects.ReturnBool() { Value = false, Reason = "Old password incorrect!" }; }
            }
            else { return new Objects.ReturnBool() { Value = false }; }
        }
    }
}
